//
//  EFShareStyleViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/15.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFShareStyleViewController.h"
#import "EFShareItem.h"
#import <UMSocialCore/UMSocialCore.h>
#import "iToast.h"

@interface EFShareStyleViewController ()


@property(strong,nonatomic)UIScrollView* scrollView;

@property(strong,nonatomic)UIImageView* shareIV;

@property(strong,nonatomic)UIView* bottomView;
@property(strong,nonatomic)UILabel* titleLabel;
@property(strong,nonatomic)UILabel* descLabel;
@property(strong,nonatomic)UILabel* areaLabel;
@property(strong,nonatomic)UILabel* scaleLabel;
@property(strong,nonatomic)UIImageView* goodsIV;

@property(strong,nonatomic)NSString* productId;
@property(assign,nonatomic)EShareType shareType;


@property(strong,nonatomic)EFShareItem* info;
@property(strong,nonatomic)NSString* shareImage;


@end

@implementation EFShareStyleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(instancetype)init{
    self = [super init];
    if (self) {
        self.title = @"选择分享样式";
    }
    return self;
}

-(instancetype)initWithProductId:(NSString *)productId type:(EShareType)type{
    self = [self init];
    if(self){
        self.productId = productId;
        self.shareType = type;
        [self initView];
        [self loadShareInfo];
    }
    return self;
}

-(void)initView{
    self.view.backgroundColor = RGBCOLOR(0xF6FAFA);
    [self.view addSubview:self.scrollView];
    [self.bottomView addSubview:self.titleLabel];
    [self.bottomView addSubview:self.descLabel];
    [self.bottomView addSubview:self.scaleLabel];
    [self.bottomView addSubview:self.goodsIV];
    [self.scrollView addSubview:self.shareIV];
    [self.scrollView addSubview:self.bottomView];
}

- (void)loadShareInfo{
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getShareInfoWithProductId:self.productId OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if (code == 0) {
            weakSelf.info = [EFShareItem yy_modelWithDictionary:info];
            [weakSelf setUI];
        }
    }];
}

- (void)setUI{
    if (self.info) {
        [self.shareIV setImageWithURL:[NSURL URLWithString:self.info.img_path]];
        [self.goodsIV setImageWithURL:[NSURL URLWithString:self.info.qrcode_img]];
        [self.titleLabel setText:self.info.title];
        self.titleLabel.numberOfLines = 0;
        [self.titleLabel sizeToFit];
        [self.descLabel setText:self.info.content];
        self.descLabel.numberOfLines = 0;
        self.descLabel.top = self.titleLabel.bottom + 4;
        [self.descLabel sizeToFit];
        
    }
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight )];
        _scrollView.contentSize = CGSizeMake(kScreenWidth , _scrollView.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIImageView *)shareIV{
    if(_shareIV == NULL){
        _shareIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 382)];
        _shareIV.left = 40;
        _shareIV.top = 40;
        _shareIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickShare:)];
        [_shareIV addGestureRecognizer:tapGesture];
        _shareIV.userInteractionEnabled = YES;
        _shareIV.contentMode = UIViewContentModeScaleAspectFill;
        _shareIV.clipsToBounds = YES;
    }
    return _shareIV;
}

-(UIView *)bottomView{
    if(_bottomView == NULL){
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 100)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.left = 40;
        _bottomView.top = self.shareIV.bottom + 10;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickShare:)];
        [_bottomView addGestureRecognizer:tapGesture];
        _bottomView.userInteractionEnabled = YES;
    }
    return _bottomView;
}

-(UIImageView *)goodsIV{
    if(_goodsIV == NULL){
        _goodsIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 67 , 72)];
        _goodsIV.left = self.bottomView.width - 37 - 67;
        _goodsIV.top = 15;
        _goodsIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _goodsIV.contentMode = UIViewContentModeScaleAspectFill;
        _goodsIV.clipsToBounds = YES;
    }
    return _goodsIV;
}


-(UILabel *)titleLabel{
    if(_titleLabel == NULL){
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bottomView.width - 37 - 67 - 10 , 0)];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = kBlack333;
        _titleLabel.left =  10;
        _titleLabel.top = 15;
        //_titleLabel.width = self.titleLabel.left - self.goodsIV.left;
    }
    return _titleLabel;
}


-(UILabel *)descLabel{
    if(_descLabel == NULL){
        _descLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bottomView.width - 67 - 35 - 10 , 0)];
        _descLabel.font = [UIFont systemFontOfSize:13];
        _descLabel.textColor = kBlack999;
        _descLabel.left =  10;
    }
    return _descLabel;
}

-(UILabel *)scaleLabel{
    if(_scaleLabel == NULL){
        _scaleLabel = [[UILabel alloc] init];
        _scaleLabel.left = 35;
        _scaleLabel.top = self.descLabel.bottom + 2;
    }
    return _scaleLabel;
}



- (void)onClickShare:(UITapGestureRecognizer *)tap{
   // [[iToast makeText:@"分享"]show];
    
    if(tap.view == self.shareIV){
        UMSocialPlatformType platformType;
        switch (self.shareType) {
            case EShareTypeWx:{
                platformType = UMSocialPlatformType_WechatSession;
                break;
            }
            case EShareTypeCircle:{
                platformType = UMSocialPlatformType_WechatTimeLine;
                break;
            } case EShareTypeWb:{
                platformType = UMSocialPlatformType_Sina;
                break;
            }
        }
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        
        //创建图片内容对象
        UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
        //如果有缩略图，则设置缩略图
       // shareObject.thumbImage = [UIImage imageNamed:@"content_icon_location"]; [UIImage imageNamed:@"test.jpg"]
        [shareObject setShareImage:self.shareIV.image];
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                NSLog(@"************Share fail with error %@*********",error);
            }else{
                NSLog(@"response data is %@",data);
            }
        }];
    }else{
        
        NSString *snsName;
        NSString *shareText;
        
        NSString *shareUrl = self.info.url;

        self.shareImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: self.info.qrcode_img]]];
        UMSocialPlatformType platformType;
        NSString * shareChannel;
        switch (self.shareType) {
            case EShareTypeWx:{
                platformType = UMSocialPlatformType_WechatSession;
                break;
            }
            case EShareTypeCircle:{
                platformType = UMSocialPlatformType_WechatTimeLine;
                break;
            } case EShareTypeWb:{
                
                snsName = @"sina";
                shareText = [NSString stringWithFormat:@"%@",self.info.title];
                if (shareText.length >= 140){
                    shareText = [shareText substringToIndex:140];
                }
                platformType = UMSocialPlatformType_Sina;
                break;
            }
        }
        NSMutableString *shareContentFinal = [[NSMutableString alloc] init];
        if (self.info.content.length>0)
        {
            if (self.info.content.length > 140)
            {
                [shareContentFinal appendString:[self.info.content substringToIndex:140]];
            }
            else
            {
                [shareContentFinal appendString:self.info.content];
            }
        }
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //设置文本
        messageObject.title = self.info.title;
        messageObject.text = shareContentFinal;
        UMShareWebpageObject *shareObj = [UMShareWebpageObject shareObjectWithTitle:self.info.title descr:shareContentFinal thumImage:_shareImage];
        shareObj.webpageUrl = shareUrl;
        messageObject.shareObject = shareObj;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                //[self getShareData:nil message:@"分享失败"];
            }else{
                //[self getShareData:shareChannel message:@"分享成功"];
            }
        }];
    }
}




@end
